package com.test.controller;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.JSONToken;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.test.model.Users;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Path("/Reg")
public class regController extends Controller {

    @Clear
    public void index(){
        render("reg.html");
    }
    @Clear
    public void doReg() {
        String username = getPara("username");
        String password = getPara("password");
        JSONObject json = new JSONObject();
        Users user = new Users();
        List<Users> users = Users.dao.find("select * from sys_users where user_name = ?", username);
        if(users.size() > 0) {
            json.put("code", -1);
            json.put("msg", "注册失败, 该用户名已存在");
            renderJson(json);
        } else {
            user.set("user_name", username);
            user.set("pass_word", password);
            user.set("create_time", new Date());
            boolean save = user.save();
            if(save) {
                json.put("code", 0);
                json.put("msg", "注册成功");
            } else {
                json.put("code", -1);
                json.put("msg", "登陆失败, 用户名已存在");
            }
            renderJson(json);
        }
//        System.out.println("===================");
    }
    public void logOut() {
        HttpServletRequest request = getRequest();
        HttpSession session = request.getSession();
        System.out.println(session);
        render("/login/login.html");
    }
}
